package com.yyfax.pay.vo.order;

import com.yyfax.framework.common.Log;
import com.yyfax.framework.yyfax.enums.CurrencyEnum;
import com.yyfax.pay.common.enums.DataSrcEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

public class ProtocolPayReq implements Serializable {

    @NotBlank(message = "orderId不能为空")
    private String orderId;

    @NotBlank(message = "userId不能为空")
    private String userId;

    @NotBlank(message = "userIp不能为空")
    private String userIp;

    @NotNull(message = "orderTime不能为空")
    private Date orderTime;

    private Integer dataSrc;

    @NotNull(message = "terminal不能为空")
    private Integer terminal;

    @NotNull(message = "amount不能为空")
    @Min(value = 1L,message = "amount必须大于0")
    private Long amount;

    private Integer currency;

    @NotBlank(message = "payNotifyUrl不能为空")
    private String payNotifyUrl;

    @NotBlank(message = "paymentCode不能为空")
    private String paymentCode;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Date getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    public Integer getDataSrc() {
        return dataSrc;
    }

    public void setDataSrc(Integer dataSrc) {
        this.dataSrc = dataSrc;
    }

    public Integer getTerminal() {
        return terminal;
    }

    public void setTerminal(Integer terminal) {
        this.terminal = terminal;
    }

    public Long getAmount() {
        return amount;
    }

    public void setAmount(Long amount) {
        this.amount = amount;
    }

    public Integer getCurrency() {
        return currency;
    }

    public void setCurrency(Integer currency) {
        this.currency = currency;
    }

    public String getPayNotifyUrl() {
        return payNotifyUrl;
    }

    public void setPayNotifyUrl(String payNotifyUrl) {
        this.payNotifyUrl = payNotifyUrl;
    }

    public String getPaymentCode() {
        return paymentCode;
    }

    public void setPaymentCode(String paymentCode) {
        this.paymentCode = paymentCode;
    }

    public String getUserIp() {
        return userIp;
    }

    public void setUserIp(String userIp) {
        this.userIp = userIp;
    }

    public void checkValue() {
        final String OP = "ProtocolPayReq.checkValue";
        if (null == this.getCurrency()) {
            this.setCurrency(Integer.valueOf(CurrencyEnum.RMB.getValue()));
        }
        if (null == this.getDataSrc()) {
            this.setDataSrc(DataSrcEnum.BUSINESS.getValue());
        }
        if (this.getCurrency() != Integer.valueOf(CurrencyEnum.RMB.getValue())) {
            logger.error(Log.op(OP).msg("currnecy参数异常").kv("currency", this.getCurrency()).toString());
            throw PayExceptionCode.ERROR_ILL_PARAMETER.exp();
        }
        if (this.getDataSrc() != DataSrcEnum.BUSINESS.getValue()) {
            logger.error(Log.op(OP).msg("dataSrc参数异常").kv("dataSrc", this.getDataSrc()).toString());
            throw PayExceptionCode.ERROR_ILL_PARAMETER.exp();
        }
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
